package com.yjc.springyjcdemo.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

/**
 * Result.  公共响应包装
 * @author
 */
@Setter
@Getter
@ToString
public class Result<T> {

    private static final int SUCCESS = 1;
    private static final int ERROR = 0;

    private static final Result<?> successResult = new Result<>();
    private static final Result<?> failedResult = new Result<>(0, "error");


    private int code;
    private String message;
    private T data;

    private Result() {
        this.code = 1;
        this.message = "success";
    }

    private Result(int code, String message) {
        this.code = code;
        this.message = message;
    }


    /**
     * 状态码兼容
     */
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result<?> ok() {
        return successResult;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> respData = new Result<>();
        respData.setCode(1);
        respData.setData(data);
        return respData;
    }

    public static Result<?> fail(String message) {
        Result<?> respData = new Result<>();
        respData.setCode(0);
        respData.setMessage(message);
        return respData;
    }

    public static Result<?> fail(int code, String message) {
        Result<?> respData = new Result<>();
        respData.setCode(code);
        respData.setMessage(message);
        return respData;
    }

    public static <T> Result<T> success() {
        return success(null, "请求成功");
    }

    public static <T> Result<T> success(T data) {
        return success(data, "成功");
    }

    public static <T> Result<T> success(T data, String msg) {
        Result<T> result = new Result<T>();
        result.setCode(CodeMsg.SUCCESS);
        if (msg == null) {
            result.setMessage(CodeMsg.getMsg(CodeMsg.SUCCESS));
        } else {
            result.setMessage(msg);
        }
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(int code, String msg, T data) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        if (msg == null) {
            result.setMessage(CodeMsg.getMsg(CodeMsg.SUCCESS));
        } else {
            result.setMessage(msg);
        }
        result.setData(data);
        return result;
    }

    /**
     * 断言count大于0，成功否则失败
     *
     * @param count
     * @return
     */
    public static Result countAssert(int count) {
        return countAssert(count, "操作成功", "操作失败");
    }

    public static Result countAssert(int count, String errMsg) {
        return countAssert(count, "操作成功", errMsg);
    }

    public static Result countAssert(int count, String okMsg, String errMsg) {
        Result result = new Result();
        result.setCode(count > 0 ? CodeMsg.SUCCESS : CodeMsg.ALERT_MSG);
        result.setMessage(count > 0 ? okMsg : errMsg);
        result.setData(count);
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<T>();
        result.setCode(CodeMsg.ALERT_MSG);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> error(int code) {
        return error(code, CodeMsg.getMsg(code));
    }

    public static <T> Result<T> error(int code, String errMsg) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMessage(errMsg);
        return result;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
